package com.facesteel.speech_play.utils;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.util.Log;
import android.widget.Toast;

import com.facesteel.speech_play.SpeechPlayHelper;

import java.lang.ref.WeakReference;

public class SensorUtil {
    private static final String TAG = "SensorUtil";
    private static final float PROXIMITY_THRESHOLD = 5.0f; // 距离阈值（厘米）

    private static SensorManager sensorManager;
    private static Sensor proximitySensor;
    private static boolean isNear = false;

    // 传感器事件监听器
    private static final SensorEventListener sensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (event.sensor.getType() == Sensor.TYPE_PROXIMITY) {
                float distance = event.values[0];

                // 当距离小于阈值且当前状态为未靠近时
                if (distance < PROXIMITY_THRESHOLD && !isNear) {
                    isNear = true;

                    if(SpeechPlayer.isBluetooth()){
                        //当前为蓝牙播放
                        Log.d(TAG, "当前为蓝牙播放");
                        return;
                    }

                    handleProximityNear();
                }
                // 当距离大于阈值且当前状态为靠近时
                else if (distance >= PROXIMITY_THRESHOLD && isNear) {
                    isNear = false;
                    if(SpeechPlayer.isBluetooth()){
                        Log.d(TAG, "当前为蓝牙播放");
                        return;
                    }
                    handleProximityFar();
                }
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {
            // 可不处理
        }
    };
    public static void initSensor(Context context) {
        if (sensorManager == null) {
            sensorManager = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        }
        if (proximitySensor == null) {
            proximitySensor = sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
        }
        if (proximitySensor != null) {
            // 注册监听，采样率使用SENSOR_DELAY_NORMAL
            sensorManager.registerListener(
                    sensorEventListener,
                    proximitySensor,
                    SensorManager.SENSOR_DELAY_NORMAL
            );


        } else {
            Log.w(TAG, "本设备不支持距离传感器");
        }
    }

    private static void handleProximityNear() {
        Log.d(TAG, "检测到靠近设备");
        if (SpeechPlayer.getMediaPlayer() != null && SpeechPlayer.getMediaPlayer().isPlaying()) {
//            SpeechPlayer.restartSpeech();
            // 切换为听筒模式（如果需要）
            SpeakerUtil.Companion.switchSpeaker(ContextHelper.getInstance().getContext(), false);

            ScreenUtil.Companion.setScreenOff(ContextHelper.getInstance().getContext());

            SpeechPlayHelper.getInstance().onSwitchSpeaker(false);
        }
    }

    private static void handleProximityFar() {
        Log.d(TAG, "检测到离开设备");
        if (SpeechPlayer.getMediaPlayer() != null && SpeechPlayer.getMediaPlayer().isPlaying()) {
//            SpeechPlayer.getMediaPlayer().start();
            // 切换回扬声器
            SpeakerUtil.Companion.switchSpeaker(ContextHelper.getInstance().getContext(), true);

            ScreenUtil.Companion.setScreenOn();

            SpeechPlayHelper.getInstance().onSwitchSpeaker(true);
        }
    }

    public static void unRegisterListener(){
        // 注销传感器监听
        if (sensorManager != null && proximitySensor != null) {
            sensorManager.unregisterListener(sensorEventListener);
//            sensorManager = null;
            proximitySensor = null;
        }
    }

    public static void initSpeaker(){
        SpeakerUtil.Companion.switchSpeaker(ContextHelper.getInstance().getContext(), !isNear);
    }

    public static boolean getNear(){
        return isNear;
    }

    public static void switchSpeaker(Context context,boolean enable) {
        SpeakerUtil.Companion.switchSpeaker(context, enable);

        isNear = !enable;
    }

}
